package my.queue2stack;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @DESCRIPTION:
 * @USER: shg
 * @DATE: 2022/12/19 23:17
 */
public class Queue2Stack {

    private Queue<Integer> queueA = new LinkedList<>();
    private Queue<Integer> queueB = new LinkedList<>();

    // 入栈
    public void push(Integer x){
        if(queueA.isEmpty() && !queueB.isEmpty()){
            queueA.add(x);
            while (!queueB.isEmpty()){
                queueA.add(queueB.poll());
            }
        }

        if(!queueA.isEmpty() && queueB.isEmpty()){
            queueB.add(x);
            while (!queueA.isEmpty()){
                queueB.add(queueA.poll());
            }
        }

        queueA.add(x);
    }

    // 出栈
    public Integer pop(){
        if(!queueA.isEmpty() && queueB.isEmpty()){
            return queueA.poll();
        }
        return queueB.poll();
    }

    // 栈顶
    public Integer top(){
        if(!queueA.isEmpty() && queueB.isEmpty()){
            return queueA.peek();
        }
        return queueB.peek();
    }

    // 是否为null
    public boolean isEmpty(){
        if(queueA.isEmpty() && queueB.isEmpty()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Queue2Stack queue2Stack = new Queue2Stack();
        queue2Stack.push(1);
        queue2Stack.push(2);
        queue2Stack.push(3);
        System.out.println(queue2Stack.pop());
        System.out.println(queue2Stack.top());
        System.out.println(queue2Stack.pop());
        System.out.println(queue2Stack.isEmpty());
    }
}
